﻿using System;
using System.IO;
using System.Windows;
using MyFloridaHouse.libSvKMZ;
using MyFloridaHouse.libSvKMZ.KML;
using MyFloridaHouse.libSvOpenDistricts.Census;
using MyFloridaHouse.libSvOpenDistricts.Dialogs;
using MyFloridaHouse.libSvOpenDistricts.Districts;
using MyFloridaHouse.libSvOpenDistricts.enumerations;
using MyFloridaHouse.libSvUtilities.Logging;

namespace MyFloridaHouse.libSvOpenDistricts.ViewModel
{
    public partial class ViewModelBase
    {
        public PlanInfoDlg planLoadPlanInfoDlg;

        private void DistrictPlanLoadReset(string planName, string planDescription, int districtCount, string planTypeID, string planTypeName)
        {
            districtPlan.InitializeDistrictPlan(planName, planDescription, districtCount, planTypeID, planTypeName);
            dataGridManager.Reset(districtCount, districtPlan.districtList);
            InitializeDistrictShapes();
            MessageBox.Show("Initialized plan with " + districtPlan.districtCount.ToString() + " districts");
        }

        private Stream importFileStream = null;
        private void DistrictPlanLoadFile(Stream fileDataStream, string fileExtension)
        {
            string formatType = fileExtension.ToUpper().Replace('.', ' ').Trim();
            if (formatType == enumDistrictPlanFormatType.KMZ.ToString())
            {
                
                MemoryStream DOJstream = null;
                MemoryStream KMLstream = null;
                bool result = false;
                try
                {

                    kml newKml = KMZ.ExtractKML(fileDataStream, ref DOJstream, ref KMLstream);
                    DistrictPlanInfoDTO planInfo = DistrictPlanDataContract.ExtractPlanInfo(newKml);
                    districtPlan.InitializeDistrictPlan(planInfo.planName, planInfo.planDescription, planInfo.districtCount, planInfo.planTypeID, planInfo.planTypeName);


                    using (StreamReader importFileStreamReader = new StreamReader(DOJstream))
                    {
                        result = districtPlan.LoadDistrictPlanFromDOJ(importFileStreamReader, planInfo.districtCount);
                    }

                    dataGridManager.Reset(planInfo.districtCount, districtPlan.districtList);
                    InitializeDistrictShapes();
                    districtPlan.ExtractKML(newKml);
                    ReloadDistrictShapesAll();
                    MessageBox.Show("Loaded plan with " + districtPlan.districtCount.ToString() + " districts");
                }
                catch (Exception ex)
                {
                    Log.HandleInternalError("Unable to read KMZ file: " + ex.Message);
                }
                finally
                {

                    if (KMLstream != null)
                    {
                        KMLstream.Close();
                    }
                    if (DOJstream != null)
                    {
                        DOJstream.Close();
                    }
                }

            }
            else if (formatType == enumDistrictPlanFormatType.DOJ.ToString())
            {
                this.importFileStream = fileDataStream;
                planLoadPlanInfoDlg = new PlanInfoDlg(OnMemberPropertyChanged, DisplayPanelForDialogs, districtPlanTypes);
                planLoadPlanInfoDlg.Start(DistrictPlanLoadDOJAfterInfoDialog);
            }
            else if (formatType == enumDistrictPlanFormatType.ZIP.ToString())
            {
                this.importFileStream = fileDataStream;
                planLoadPlanInfoDlg.Start(DistrictPlanLoadZIPFileAfterInfoDialog);
            }
            else
            {
                MessageBox.Show("Sorry, don't know how to import files of type [ " + formatType + "]");
                {
                    if (importFileStream != null)
                    {
                        importFileStream.Close();
                    }
                }
            }
        }




        private void DistrictPlanLoadDOJAfterInfoDialog(bool result)
        {
            if (result)
            {
                DistrictPlanLoadDOJFile(planLoadPlanInfoDlg.planName, planLoadPlanInfoDlg.planDescription, planLoadPlanInfoDlg.planDistrictCount, planLoadPlanInfoDlg.planTypeID, planLoadPlanInfoDlg.planTypeName);
            }
            planLoadPlanInfoDlg = null;
        }

        private void DistrictPlanLoadZIPFileAfterInfoDialog(bool result)
        {
            try
            {
                if (result)
                {
                    throw new NotImplementedException();
                    //todo: result = unzip  : Need to unzip the DOJ file
                    // then put the 


                    //DistrictPlanLoadDOJFile(result);

                }
            }
            catch (Exception ex)
            {
                Log.HandleException(ex);
            }
            finally
            {
                if (importFileStream != null)
                {
                    importFileStream.Close();
                }
            }
            planLoadPlanInfoDlg = null;
        }

        
        public void DistrictPlanLoadDOJFile(string planName, string planDescription, int planDistrictCount, string planTypeID, string planTypeName)
        {
            try
            {

                if (importFileStream == null)
                {
                    MessageBox.Show("Sorry, empty file ");
                    return;
                }
                else
                {

                    using (StreamReader importFileStreamReader = new StreamReader(importFileStream))
                    {
                        if (importFileStreamReader == null)
                        {
                            MessageBox.Show("Sorry, cannot read file ");
                            return;
                        }
                        else
                        {
                            districtPlan.InitializeDistrictPlan( planName, planDescription, planDistrictCount, planTypeID, planTypeName);
                            bool result = districtPlan.LoadDistrictPlanFromDOJ(importFileStreamReader, planDistrictCount);
                            dataGridManager.Reset(districtPlan.districtCount, districtPlan.districtList);
                            InitializeDistrictShapes();
                            BuildDistrictShapesAll();
                        }
                    }
                }
            }


            catch (Exception ex)
            {
                Log.HandleException(ex);
            }
            finally
            {
                if (importFileStream != null)
                {
                    importFileStream.Close();
                }
            }
        }
    }
}
